<?php
$isReloadable = true;
$isPopUp = true;
require_once("../../pers/inc.config.php");
require_once(OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php');

reqDef('vismod','vis');
$sListaTipiImp = $objSess->getSV('agenda','listTI');
if ($sListaTipiImp=='') {
   $sListaTipiImp = $objSess->aSV['agenda']['listTI'] = 'all';
}
$aTimp = explode(",",$sListaTipiImp);
$bReloadCB = true;
switch (reqVar('azione')) {
   case 'salva':
	  $msgPag .= salva();
     break;
   case 'conferma':
   	$sViewMemo = (reqVar('viewMemo')=='S')?'S':'N';
      if (reqVar('set_all_timp')=='S') {
         $sListaTipiImp = "all";
      } else {
      	$sListaTipiImp = "0";
      	foreach($_REQUEST as $nX => $vX) {
      		if (substr($nX,0,8)!='cb_timp_') continue;
      		$sListaTipiImp .= ','.substr($nX,8);
      	}
      	if ($sListaTipiImp == "0" && $sViewMemo == 'N') {
      		$msgPag .= _t("seleziona almeno un tipo impegno");
            $bReloadCB = false;
            break;
      	} 
      }
		echo "chiusura in corso ...";	
		echo "<script>\n";
		echo "window.opener.location.href='agenView.php?viewMemo=$sViewMemo&listTI=$sListaTipiImp&$sXSNQS';\n";
		echo "window.close();\n";
		echo "</script>\n";
		exit();
   case 'setmod':
      $_REQUEST['vismod'] = 'mod';
      break;
   case 'setvis':
      $_REQUEST['vismod'] = 'vis';
      break;
}
if ($bReloadCB) {
   foreach($aTimp as $idX) {
      reqDef('cb_timp_'.$idX,'S');
   }
   $objSess->setReqSV('agenda', 'viewMemo', 'S');
}

$sSql = "SELECT * FROM ovf_agenda_tipi_imp ORDER BY descr";
$objDB->query($sSql,'selTI');

$sCmdL  = "";

$sCmdR  = "&nbsp;&nbsp;&nbsp";
if (reqVar('vismod')=='mod') {
	$sCmdR .=  "<a class='cmd1' href=\"javascript:submAz('salva');\">"._t("salva")."</a>";
	$sCmdR .= "&nbsp;&nbsp;&nbsp";
	$sCmdR .=  "<a class='cmd1' href=\"javascript:submAz('setvis');\">"._t("annulla")."</a>";
	$sCmdR .= "&nbsp;&nbsp;&nbsp";
} else {
   $sCmdL .=  "<a class='cmd1' href=\"javascript:submAz('setmod');\">"._t("aggiungi/modifica")."</a>";
   $sCmdR .= "&nbsp;&nbsp;&nbsp";
   $sCmdR .=  "<a class='cmd1' href=\"javascript:submAz('conferma');\">"._t("conferma")."</a>";
   $sCmdR .= "&nbsp;&nbsp;&nbsp";
}
$sCmdR .=  "<a class='cmd1' href='javascript:window.close();'>"._t("chiudi")."</a>";
$aOpt = array("width"=>600);
echo getTestaPopUpHtml("Selezione tipo impegno",$msgPag, $aOpt);
?>
<script type="text/javascript">
function submAz(az) {
	document.frm1.azione.value=az;
	document.frm1.submit();
}
</script>
<?
echo getHtmlForm();
echo getHtmlHidden('xsn');
echo getHtmlHidden('azione','...');
echo getHtmlHidden('vismod');
$sTxtX = 'txt1';
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
   <tr>
      <td class="tit1" align="center" colspan='2'>
         Tipi impegno 
      </td>
   </tr>
</table>
<?
require "_inc/inc.palliniTipoImp.php";
if (reqvar('vismod')=='mod') { //form modifica
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" colspan='3'><?=_t("Tipi impegno")?></td>
   </tr>
<?
   while($acTI = $objDB->fetch('selTI')) {
      $sTxtX = ($sTxtX=='txt1')?'txt2':'txt1';
      if ($acTI['pallino']=='') {
         $sPallino = "&nbsp;";
      } else {
         $sPallino = "<img src=\"".OVF_URL_ROOT."/app/_img/pallino_ti_".$acTI['pallino'].".gif\" border='0' />";
      }
?>
   <tr>
      <td class="<?php echo $sTxtX?>" colspan='3'>
         <? 
         reqDef('ti_des_'.$acTI['id_tipo_imp'],$acTI['descr']);
         reqDef('ti_pal_'.$acTI['id_tipo_imp'],$acTI['pallino']);
         echo getHtmlText('ti_des_'.$acTI['id_tipo_imp'],40);
         echo getHtmlSelect('ti_pal_'.$acTI['id_tipo_imp'],$aPal);
         ?>
      </td>
   </tr>
<?
   }
?>
   <tr>
      <td class="<?php echo $sTxtX?>" colspan='3'>
         <? 
         echo getHtmlText('new_ti_des',40);
         echo getHtmlSelect('new_ti_pal',$aPal);
         ?>
      </td>
   </tr>
</table>
<?
} else { //selezione tipi imp / memo
?>
<script>
function setAllTimp() {
   oSetAllCbx = document.getElementById("set_all_timp");
   if (!oSetAllCbx) {
      alert("set_all_timp ?");
      return;
   }
   for(k=0; k<document.frm1.elements.length; k++) {
      oElem = document.frm1.elements[k];
      if (oElem.name.substring(0,8)!=('cb_timp_')) continue;
      oElem.checked = oSetAllCbx.checked;
   }
}
function checkAllTimp() {
   //alert('checkAllTimp');
   oSetAllCbx = document.getElementById("set_all_timp");
   if (!oSetAllCbx) {
      alert("set_all_timp ?");
      return;
   }
   for(k=0; k<document.frm1.elements.length; k++) {
      oElem = document.frm1.elements[k];
      if (oElem.name.substring(0,8)!=('cb_timp_')) continue;
      if (!oElem.checked) {
         oSetAllCbx.checked = false;
         return;
      } 
   }
   oSetAllCbx.checked = true;
}
</script>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="<?php echo $sTxtX?>" width='1%'>
         <? 
         if ($sListaTipiImp=='all' && $bReloadCB) {
            $_REQUEST['set_all_timp'] = 'S';
         }
         $aOptSA = array("onClick"=>"setAllTimp();");
         echo getHtmlCheckBox('set_all_timp', 'S','@REQ',$aOptSA);?>
      </td>
      <td class="lab1" colspan='1' width='1%'>&nbsp;</td>
      <td class="lab1" colspan='1' width='99%'><?=_t("Tipi impegno")?></td>
   </tr>
<?php 
   require "_inc/inc.palliniTipoImp.php";
   $aOptCB = array("onClick"=>"checkAllTimp();");
   while($acTI = $objDB->fetch('selTI')) {
      $sTxtX = ($sTxtX=='txt1')?'txt2':'txt1';
      if ($acTI['pallino']=='') {
         $sPallino = "&nbsp;";
      } else {
         $sPallino = "<img src=\"".OVF_URL_ROOT."/app/_img/pallino_ti_".$acTI['pallino'].".gif\" border='0' />";
      }
?>
   <tr>
      <td class="<?php echo $sTxtX?>" >
      <? 
      if ($sListaTipiImp=='all' && $bReloadCB) {
         $_REQUEST['cb_timp_'.$acTI['id_tipo_imp']] = 'S';
      }
      echo getHtmlCheckBox('cb_timp_'.$acTI['id_tipo_imp'], 'S','@REQ',$aOptCB);?>
      </td>
      <td class="<?php echo $sTxtX?>" >
         <?=$sPallino?>
      </td>
      <td class="<?php echo $sTxtX?>" >
         <? echo $acTI['descr'];?>
      </td>
   </tr>
<?
   }
?>
   <tr>
      <td class="<?php echo $sTxtX?>" >
         <? echo getHtmlCheckBox('viewMemo', 'S');?>
      </td>
      <td class="<?php echo $sTxtX?>" >
         &nbsp;
      </td>
      <td class="<?php echo $sTxtX?>" >
         <? echo _t("MEMO");?>
      </td>
   </tr>
</table>
<?
}
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<?php 
echo "</form>\n";
echo getCodaPopUpHtml();

function salva() {
	global $objSess, $objDB;
	$msg = '';
   //return print_r($_REQUEST,1);
	foreach($_REQUEST as $nX => $vX) {
	   if (substr($nX,0,7)!='ti_des_') continue;
      if (trim($vX)!='') {
         $idTimp = substr($nX,7);
         $sSql = "UPDATE ovf_agenda_tipi_imp SET descr = '".reqVarAS('ti_des_'.$idTimp)."' "
      		.", pallino = '".reqVarAS('ti_pal_'.$idTimp)."'"
      		." WHERE id_tipo_imp = ".$idTimp;
      	$objDB->exec($sSql);
      }
   }
   
	if (reqVarTrim('new_ti_des')!='') {
      $sSql = "INSERT INTO ovf_agenda_tipi_imp SET descr = '".reqVarAS('new_ti_des')."' "
         .", pallino = '".reqVarAS('new_ti_pal')."'";
      $objDB->exec($sSql);
   }
   $_REQUEST['vismod']='vis';
	return '';
}
